package com.tongqu.handlers;

import com.tongqu.entity.Login;
import com.tongqu.entity.User;
import com.tongqu.message.LoginMessage;
import com.tongqu.message.Message;
import com.tongqu.message.ReturnMessage;
import com.tongqu.protocol.TestUser;
import com.tongqu.service.TableService;
import com.tongqu.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;


@Slf4j
@Component
@ChannelHandler.Sharable
public class UserChannel extends SimpleChannelInboundHandler<LoginMessage> {
    @Autowired
    private UserService userService;

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, LoginMessage message) throws Exception {
        System.out.println("开始测试");


        int userExit = userService.findUserExit(message.getAccount().getAcct());
        if (userExit>0){
            System.out.println("账号存在");
            User user = userService.findUser(message.getAccount());
            System.out.println(user);
            if (user!=null){
                ReturnMessage returnMessage = new ReturnMessage("登入成功");
                channelHandlerContext.writeAndFlush(returnMessage);
            }
            else channelHandlerContext.writeAndFlush(new ReturnMessage("密码错误"));
            log.debug("{}",user);

            return;
        }
        int i = userService.insertUser(message.getAccount());
        if (i>0){
            channelHandlerContext.writeAndFlush(new ReturnMessage("创建成功"));
        }

    }
}
